﻿/***************************************************************************
 * 文件名：BankAccValidator
 * 功能：
 * 说明：
 * -------------------------------------------------------------------------
 * 创建时间：2024/5/21 15:30:53
 * 创建人：林远
 * 邮箱： 1586099821@qq.com
 * =========================================================================
 *
 * 修改人：   
 * 修改时间：   
 * 修改说明：   
 ***************************************************************************/
using MySystem.Interface;
using MySystem.Static;
using MySystem.NetUtil;
using BankAccValidator.Interface;
using BankAccValidator.Server;
using BankAccValidator.Util;

namespace BankAccValidator
{
    /// <summary>
    /// 银行业务帮助类
    /// </summary>
    public class BankBusVerifyHelper
    {
        public static BankBusCheckResult VerifyBankBus(string bank, string bankBustype, string accountNumber)
        {
            throw new NotImplementedException();
        }

        public static BankBusCheckResult VerifyBankBus(string bank, string bankBustype, string accountNumber, string iDNumber)
        {
            throw new NotImplementedException();
        }

        public static BankBusCheckResult VerifyBankBus(string bank, string bankBustype, string accountNumber, string iDNumber, string phoneNumber)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 银行业务验证
        /// </summary>
        /// <param name="bank">开户银行</param>
        /// <param name="bankBustype">业务类型</param>
        /// <param name="accountNumber">银行账号</param>
        /// <param name="iDNumber">身份证号</param>
        /// <param name="phoneNumber">预留电话号码</param>
        /// <param name="criteria">附加信息</param>
        /// <returns></returns>
        public static BankBusCheckResult VerifyBankBus(string bank, string bankBustype, string accountNumber, string iDNumber, string phoneNumber, object criteria)
        {
            CheckArguments(bank, bankBustype, accountNumber, iDNumber, phoneNumber);

            string url = BankInterfaceUrlConst.GetBusInterfaceUrl(bank, bankBustype);

            IBusPostParam postParam = BankBusObjectFactory.CreateBusPostParam(BankInterfaceUrlConst.GetBusPostParamType(bank, bankBustype), accountNumber, iDNumber, phoneNumber, criteria);

            var postResult = BankBusRequestHelper.PostBus(url, postParam, BankInterfaceUrlConst.GetBusResultType(bank, bankBustype));

            var checkObj = postResult as IBusSelfcheckable;

            if (checkObj == null)
            {
                throw new NullReferenceException($@"银行业务类未实现接口{nameof(IBusSelfcheckable)}，请联系管理员！");
            }

            return checkObj.CheckSelf();
        }

        private static void CheckArguments(string bank, string bankBustype, string accountNumber, string iDNumber, string phoneNumber)
        {
            if (string.IsNullOrWhiteSpace(bank))
            {
                throw new ArgumentException($"银行不可为空！");
            }

            if (string.IsNullOrWhiteSpace(bankBustype))
            {
                throw new ArgumentException("业务类型不可为空！");
            }

            if (string.IsNullOrWhiteSpace(accountNumber))
            {
                throw new ArgumentException("银行卡号不可为空");
            }

            if (!accountNumber.IsBankAccountNumber())
            {
                throw new ArgumentException("银行卡号格式错误！");
            }

            if (!string.IsNullOrEmpty(iDNumber) && !iDNumber.IsIdNumber())
            {
                throw new ArgumentException("身份证号格式错误！");
            }

            if (!string.IsNullOrWhiteSpace(phoneNumber) && !phoneNumber.IsPhoneNumber())
            {
                throw new ArgumentException("电话号码格式错误！");
            }

            if (!BankBusTypeConst.ContainsKey(bankBustype))
            {
                throw new Exception($"暂不支持业务类型{bankBustype}");
            }

            if (!BankConst.ContaisKey(bank))
            {
                throw new Exception($"暂未配置银行{bank}相关业务！");
            }

            if (!BankInterfaceUrlConst.ContainsKey(bank, bankBustype))
            {
                throw new Exception($"暂未配置银行{bank}【{BankConst.GetName(bank)}】业务{bankBustype}【{BankBusTypeConst.GetName(bankBustype)}】接口！");
            }
        }
    }
}